const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    newPhone: "",
    verification: "",
    downtime: 60000,
    disabled: false,
  },

  onLoad() {
    this.setData({
      mobile: app.globalData.userInfo.userDetail.mobile,
    });
  },

  sendMsg() {
    if (!this.data.newPhone) {
      wx.showToast({
        title: "请输入新手机号",
        icon: "none",
        duration: 2000,
      });
      return;
    }
    wx.showLoading({
      title: "发送短信中",
    });
    wx.$apis.sendSms(this.data.newPhone, 7).then((res) => {
      wx.showToast({
        title: res.data,
        icon: "none",
        duration: 2000,
      });
      this.setData({
        disabled: true,
      });
      const countDown = this.selectComponent(".control-count-down");
      countDown.start();
    });
  },

  finished() {
    this.setData({
      disabled: false,
      downtime: 60000,
    });
  },

  onSubmit() {
    if (!this.data.newPhone) {
      wx.showToast({
        title: "请输入新手机号",
        icon: "none",
        duration: 2000,
      });
      return;
    }
    if (!this.data.verification) {
      wx.showToast({
        title: "请输入验证码",
        icon: "none",
        duration: 2000,
      });
      return;
    }
    wx.showLoading({
      title: "提交中...",
    });
    wx.$apis.updatePhone(this.data.newPhone, this.data.verification).then((res) => {
      wx.showToast({
        title: "修改成功",
        icon: "success",
        duration: 1000,
      }).then((r) => {
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/management/center/center",
          });
        }, 1000);
      });
    });
  },
});
